package com.jlwl.houseservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jlwl.houseservice.entity.Article;
import com.jlwl.houseservice.entity.ArticleCategory;
import com.jlwl.houseservice.mapper.ArticleMapper;
import com.jlwl.houseservice.service.ArticleCategoryService;
import com.jlwl.houseservice.service.ArticleService;
import com.jlwl.houseservice.vo.ArticleDetailVO;
import com.jlwl.houseservice.vo.ArticleQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 文章Service实现类
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Override
    public Page<Article> pageList(ArticleQueryVO queryVO) {
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        
        // 关键词搜索
        if (StringUtils.hasText(queryVO.getKeyword())) {
            queryWrapper.and(wrapper -> wrapper
                    .like(Article::getTitle, queryVO.getKeyword())
                    .or()
                    .like(Article::getContent, queryVO.getKeyword())
            );
        }
        
        // 分类筛选
        if (queryVO.getCategoryId() != null) {
            queryWrapper.eq(Article::getCategoryId, queryVO.getCategoryId());
        }
        
        // 状态筛选
        if (queryVO.getStatus() != null) {
            queryWrapper.eq(Article::getStatus, queryVO.getStatus());
        } else {
            // 默认只查询上架的文章
            queryWrapper.eq(Article::getStatus, 1);
        }
        
        // 默认按创建时间倒序排序
        queryWrapper.orderByDesc(Article::getCreatedAt);
        
        // 分页查询
        return page(new Page<>(queryVO.getPage(), queryVO.getSize()), queryWrapper);
    }

    @Override
    public ArticleDetailVO getArticleDetail(Integer id) {
        // 获取文章信息
        Article article = getById(id);
        if (article == null) {
            return null;
        }
        
        // 获取分类信息
        ArticleCategory category = articleCategoryService.getById(article.getCategoryId());
        
        // 构建返回结果
        ArticleDetailVO detailVO = new ArticleDetailVO();
        detailVO.setArticle(article);
        detailVO.setCategory(category);
        
        return detailVO;
    }

    @Override
    @Transactional
    public boolean incrementViewCount(Integer id) {
        return update(new LambdaUpdateWrapper<Article>()
                .eq(Article::getId, id)
                .setSql("view_count = view_count + 1"));
    }

    @Override
    public List<Article> getRecommendArticles(Integer limit) {
        return list(new LambdaQueryWrapper<Article>()
                .eq(Article::getStatus, 1)
                .orderByDesc(Article::getViewCount)
                .last("LIMIT " + limit));
    }

    @Override
    public List<Article> getArticlesByCategory(Integer categoryId, Integer limit) {
        return list(new LambdaQueryWrapper<Article>()
                .eq(Article::getCategoryId, categoryId)
                .eq(Article::getStatus, 1)
                .orderByDesc(Article::getCreatedAt)
                .last("LIMIT " + limit));
    }

    @Override
    @Transactional
    public boolean createArticle(Article article) {
        article.setViewCount(0);
        article.setCreatedAt(LocalDateTime.now());
        article.setUpdatedAt(LocalDateTime.now());
        return save(article);
    }

    @Override
    @Transactional
    public boolean updateArticle(Article article) {
        Article existArticle = getById(article.getId());
        if (existArticle == null) {
            return false;
        }
        
        article.setUpdatedAt(LocalDateTime.now());
        return updateById(article);
    }

    @Override
    @Transactional
    public boolean deleteArticle(Integer id) {
        return removeById(id);
    }
} 